package com.googlecode.scup.parser.actionstates;

import com.googlecode.scup.grammar.grammarspart.Production;
import com.googlecode.scup.grammar.grammarspart.Symbol;
import com.googlecode.scup.grammar.grammarspart.AutomatRule;
import com.googlecode.scup.parser.InputStream;

import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.List;

public class ReduceAction implements ActionState {
	private AutomatRule rule;

	public ReduceAction(AutomatRule rule) {
		this.rule = rule;
	}

	@Override
	public ActionStateType getType() {
		return ActionStateType.REDUCE;
	}

    @Override
    public void doAction(Stack<Symbol> symbolsStack, Stack<Integer> states, InputStream inputStream, List<Map<Symbol, ActionState>> Goto) {
        //TODO: wtf -1 o_O
        for (int i = 0; i < rule.getItem().size(); ++i) {
            states.pop();
            symbolsStack.pop();
        }
        symbolsStack.push(rule.getBase());
        Goto.get(states.peek()).get(symbolsStack.peek())
                .doAction(symbolsStack, states, inputStream, Goto);
    }

//    @Override
//	public void doAction() {
//	}

    @Override
    public String toString() {
        return new String("r " + rule.getItem().size());
    }
}
